//
//  GalleryController.m
//  Clock
//
//  Created by Le Thanh Hai on 10/13/11.
//  Copyright 2011 __MyCompanyName__. All rights reserved.
//

#import "GalleryController.h"


@implementation GalleryController
@synthesize btnBack;

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)dealloc
{
    [super dealloc];
}

- (void)didReceiveMemoryWarning
{
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
    
    // Release any cached data, images, etc that aren't in use.
}

#pragma mark - View lifecycle

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    self.view.backgroundColor = [UIColor scrollViewTexturedBackgroundColor];
    int index = 0;
    for (int i = 0; i < 4; i++) {
        for (int j = 0; j < 3; j++) {
            UIView *img = [[UIView alloc] initWithFrame:CGRectMake(64 + j*(62+3), 58 + i*(92 + 3), 62, 93)];
            img.tag = index++;
            UIImageView *imgView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:[NSString stringWithFormat:@"character%d_%d_mini.jpg", i+1, j+1]]];
            imgView.frame = CGRectMake(0, 0, 62, 93);
            [img addSubview:imgView];
            [imgView release];
            img.backgroundColor = [UIColor blueColor];
            [self.view addSubview:img];
            [img release];
            UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(panPiece:)];
            [tapGesture setNumberOfTapsRequired:1];
            [tapGesture setDelegate:self];
            [img addGestureRecognizer:tapGesture];
            [tapGesture release];
        }
    }    
}

- (void)panPiece:(UIPanGestureRecognizer*)recognizer
{
    popview = [[PopView alloc] initWithImage:[UIImage imageNamed:[NSString stringWithFormat:@"character1_%d.jpg", recognizer.view.tag + 1]]];
    [self presentModalViewController:popview animated:YES];
    [popview release];
}

- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer
{    
    // if the gesture recognizers are on different views, don't allow simultaneous recognition
    if (gestureRecognizer.view != otherGestureRecognizer.view)
        return NO;
    return YES;
}

- (IBAction)backView
{
    [self dismissModalViewControllerAnimated:YES];
}

- (void)viewDidUnload
{
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    // Return YES for supported orientations
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}

@end
